<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="inet.db.tennis.TennisSeason"%>
<%@page import="inet.db.tennis.TennisChampion"%>
<%@page import="inet.db.tennis.TennisPhase"%>
<%@page import="inet.db.tennis.TennisPlayer"%>
<%@page import="inet.db.tennis.TennisPlayerDAO"%>
<%@page import="java.math.BigDecimal"%>
<jsp:useBean id="tennisChampionPlayerDAO" class="inet.db.tennis.TennisChampionPlayerDAO" scope="session"/>
<jsp:useBean id="tennisChampionDAO" class="inet.db.tennis.TennisChampionDAO" scope="session"/>
<jsp:useBean id="tennisSeasonDAO" class="inet.db.tennis.TennisSeasonDAO" scope="session"/>
<jsp:useBean id="tennisPhaseDAO" class="inet.db.tennis.TennisPhaseDAO" scope="session"/>
<jsp:useBean id="tennisPlayerDAO" class="inet.db.tennis.TennisPlayerDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Thêm tay vợt vào giải đấu</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_add_player_champion() {
    		if(document.formAddPlayerChampion.slbPhaseId.value=='0'){
				document.getElementById("lblPhase").innerHTML = 'Chọn giai đoạn ';
				document.formAddPlayerChampion.slbPhaseId.focus();
				return false;
			}else{
				document.getElementById("lblPhase").innerHTML = '';
			}
			
    		document.formAddPlayerChampion.action.value = "CREATE";
    		document.formAddPlayerChampion.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_add_player_champion();
		}
		function loadPhase(url, function_change, seasonId, championCode, phaseId) {
			url = url + "?championCode="+championCode+"&seasonId="+seasonId+"&phaseId="+phaseId;
		    if (window.XMLHttpRequest) { // Non-IE browsers
		      req = new XMLHttpRequest();
		      req.onreadystatechange = function_change;
		      try {
		          req.open("GET", url, true); //was get
		      } catch (e) {
		          //alert("Problem Communicating with Server\n"+e);
		      }
		      req.send(null);
		    } else if (window.ActiveXObject) { // IE
			    req = new ActiveXObject("Microsoft.XMLHTTP");
			    if (req) {
			        req.onreadystatechange = function_change;
			        req.open("GET", url, true);
			        req.send();
			    }
		    }
		 }
		 function processPhaseLoadData() {
			  //alert(req.readyState);
		      if (req.readyState == 4) { // Complete
			      if (req.status == 200) { // OK response
			      	document.getElementById("divPhase").innerHTML = req.responseText;
				  } else {
			        //alert("Problem with server response:\n " + req.statusText);
			      }
		     }else{
		     	//
		     }
	 	}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    String sAction = request.getParameter("action");
		    String sChampionCode = request.getParameter("championCode");
		    if(sChampionCode == null) sChampionCode = "";
		    
		    // Lấy thông tin giải đấu
		    TennisChampion champion = tennisChampionDAO.getRow(sChampionCode);
		    // End
		    
		    String sSeasonId = request.getParameter("slbSeason");
		    BigDecimal seasonId = null;
		    if (sSeasonId != null) seasonId = new BigDecimal(sSeasonId);
		    
		    String sPhaseId = request.getParameter("slbPhaseId");
		    BigDecimal phaseId = null;
		    if (sPhaseId != null) phaseId = new BigDecimal(sPhaseId);
		    
		    String sPlayerId = request.getParameter("slbPlayer");
		    BigDecimal playerId = null;
		    if (sPlayerId != null) playerId = new BigDecimal(sPlayerId);
		    
		    String sSeed = request.getParameter("txtSeed"); 
		    
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    if(sAction != null && "CREATE".equals(sAction)) {
		    	if(sSeasonId == null ) {
		    		sMessageWarning = "Bạn phải nhập mùa giải";
		    	} else {
		    		//String championCode, BigDecimal seasonId, BigDecimal playerId, BigDecimal phaseId, String seed
		    		boolean isOk = tennisChampionPlayerDAO.insertRow(sChampionCode, seasonId, playerId, phaseId, sSeed);
		    		if(isOk) {
		    			sMessageOk = "Thêm tay vợt vào giải đấu thành công";
		    		} else {
		    			sMessageErr = "Xảy ra lỗi khi thêm tay vợt vào giải đấu";
		    		}
		    	}
		    }
        %>
        <div class="grid_5">
            <div class="box round first fullpage">
                <h2>Thêm tay vợt vào giải đấu</h2>
                <div class="block ">
                	<% if( champion != null ) { %>
                    <form name="formAddPlayerChampion" onKeyPress="return disableEnterKey(event)" method="post">
                    	<input type="hidden" name="action"/>
                    	<input type="hidden" name="championCode" value="<%= sChampionCode %>"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                        <tr>
	                            <td class="col1">
	                                <label>Tên giải đấu</label>
	                            </td>
	                            <td class="col2">
	                                <label><%= champion.getName() %></label>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Mùa giải</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbSeason" 
	                                	onchange="loadPhase('<%= request.getContextPath() %>/admin/tennis/process/load-phase-of-season.jsp',processPhaseLoadData,document.formAddPlayerChampion.slbSeason.value,'<%= champion.getCode() %>', '<%= champion.getCurrentPhase() %>');">
				           				<%
				           					Vector<TennisSeason> vTennisSeason = tennisSeasonDAO.findAll(sChampionCode);
				           					if( vTennisSeason != null && !vTennisSeason.isEmpty()) {
				           						TennisSeason season = null;
				           						for (Iterator it = vTennisSeason.iterator(); it.hasNext(); ){
				           							season = (TennisSeason)it.next();
				           							if(season == null) continue;
				           				%>
				           				<option <%= season.getId().equals(champion.getCurrentSeason()) ? "selected":"" %> value="<%= season.getId() %>"> <%= season.getSeasonName() %></option>
				           				<%
				           						}
				           					}
				           				%>
				            		</select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Giai đoạn</label>
	                            </td>
	                            <td>
	                            	<div id="divPhase">
		                                <select id="select" name="slbPhaseId">
					           				<%
					           					Vector<TennisPhase> vTennisPhase = tennisPhaseDAO.findAll(sChampionCode, champion.getCurrentSeason());
					           					if( vTennisPhase != null && !vTennisPhase.isEmpty()) {
					           						TennisPhase phase = null;
					           						for (Iterator it = vTennisPhase.iterator(); it.hasNext(); ){
					           							phase = (TennisPhase)it.next();
					           							if(phase == null) continue;
					           				%>
					           				<option <%= phase.getId().equals(champion.getCurrentPhase()) ? "selected":"" %> value="<%= phase.getId() %>"> <%= phase.getPhaseName() %></option>
					           				<%
					           						}
					           					}
					           				%>
					            		</select>
				            		</div>
				            		<span id="lblPhase" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Cầu thủ</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbPlayer">
				           				<%
				           					Vector<TennisPlayer> vTennisPlayer = tennisPlayerDAO.findAll();
				           					if( vTennisPlayer != null && !vTennisPlayer.isEmpty()) {
				           						TennisPlayer player = null;
				           						for (Iterator it = vTennisPlayer.iterator(); it.hasNext(); ){
				           							player = (TennisPlayer)it.next();
				           							if(player == null) continue;
				           				%>
				           				<option value="<%= player.getId() %>"> <%= player.getName() %></option>
				           				<%
				           						}
				           					}
				           				%>
				            		</select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Hạt giống</label>
	                            </td>
	                            <td>
	                                <input type="text" id="grumble" name="txtSeed" />
	                            </td>
	                        </tr>
	                        <tr>
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_add_player_champion();" class="btn btn-grey">Thêm mới</button>
	                            	<button type="button" onclick="window.location.href='list-player-champion.jsp?championCode=<%= sChampionCode %>';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                    <% } else { %>
                    <div class="message warning">
                        <h5>Cảnh báo !</h5>
                        <p>
                            Không tồn tại giải đấu có Code = <%= sChampionCode %>
                        </p>
                    </div>
                    <% } %>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>